<?php

namespace slinks\extensions\templating\compiler;

use slinks\extensions\templating\TemplateExtension;
use slinks\core\dependencyInjectionExt\compiler\AbstractInterfaceProcessingPass;
use slinks\core\DependencyInjection\Reference;
use slinks\core\DependencyInjection\Definition;
use slinks\core\DependencyInjection\ContainerBuilder;
use slinks\core\dependencyInjectionExt\compiler\AbstractDefinitionProcessingPass;

/**
 * Adds Services that implement  
 * 
 * @author Navid Mitchell
 *
 */
class TemplateEngineProcessingPass extends AbstractInterfaceProcessingPass {
    const TEMPLATE_ENGINE_SERVICE = 'slinks.template.engine';

    private $templateEngineDef = null;

    function __construct() {
        parent::__construct('slinks\\core\\Templating\\EngineInterface');
    }

    /**
     * You can modify the Definition here before it is dumped to PHP code.
     *
     * @param string $id the service definition id;c
     * @param Definition $definition
     */
    public function processDefinition($id, Definition $definition) {

        $this->getEngineDefinition()->addMethodCall("addEngine", array(new Reference($id)));
    }

    /**
     * This method can be overrided  by subclasses to do some additional work after all definitions have been processed.
     *
     * @param ContainerBuilder $container
     */
    public function postProcess(ContainerBuilder $container) {
        // we only add if any valid engines were found in config.
        if ($this->templateEngineDef != null) {
            $container->setDefinition(self::TEMPLATE_ENGINE_SERVICE, $this->templateEngineDef);
        }
    }

    private function getEngineDefinition() {
        if ($this->templateEngineDef == null) {
            $this->templateEngineDef = new Definition();
            $this->templateEngineDef->setClass('slinks\\core\\Templating\\DelegatingEngine');
        }
        return $this->templateEngineDef;
    }

}